package com.aidex.biz.service.impl;

import com.aidex.biz.domain.PublishLog;
import com.aidex.biz.domain.PublishUser;
import com.aidex.biz.mapper.PublishLogMapper;
import com.aidex.biz.service.PublishLogService;
import com.aidex.biz.service.PublishUserService;
import com.aidex.common.core.service.BaseServiceImpl;
import com.aidex.common.utils.NumberUtils;
import com.aidex.common.utils.SecurityUtils;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * 发布日志Service业务层处理
 *
 * @author sean
 * @email 2074844820@qq.com
 * @date 2025-04-25
 */
@Service
@Transactional(readOnly = true)
public class PublishLogServiceImpl extends BaseServiceImpl<PublishLogMapper, PublishLog> implements PublishLogService {
    private static final Logger log = LoggerFactory.getLogger(PublishLogServiceImpl.class);
    @Autowired
    private PublishUserService publishUserService;

    /**
     * 获取单条数据
     *
     * @param publishLog 发布日志
     * @return 发布日志
     */
    @Override
    public PublishLog get(PublishLog publishLog) {
        PublishLog dto = super.get(publishLog);
        return dto;
    }

    /**
     * 获取单条数据
     *
     * @param id 发布日志id
     * @return 发布日志
     */
    @Override
    public PublishLog get(String id) {
        PublishLog dto = super.get(id);
        return dto;
    }


    /**
     * 查询发布日志列表
     *
     * @param publishLog 发布日志
     * @return 发布日志
     */
    @Override
    public List<PublishLog> findList(PublishLog publishLog) {
        return super.findList(publishLog);
    }

    /**
     * 分页查询发布日志列表
     *
     * @param publishLog 发布日志
     * @return 发布日志
     */
    @Override
    public PageInfo<PublishLog> findPage(PublishLog publishLog) {
        return super.findPage(publishLog);
    }

    /**
     * 保存发布日志
     *
     * @param publishLog
     * @return 结果
     */
    @Override
    public boolean save(PublishLog publishLog) {
        return super.save(publishLog);
    }


    /**
     * 删除发布日志信息
     *
     * @param publishLog
     * @return 结果
     */
    @Override
    public boolean remove(PublishLog publishLog) {
        return super.remove(publishLog);
    }

    /**
     * 批量删除发布日志
     *
     * @param ids 需要删除的发布日志ID
     * @return 结果
     */
    @Transactional(readOnly = false)
    @Override
    public int deletePublishLogByIds(String[] ids) {
        return mapper.deletePublishLogByIds(ids);
    }

    /**
     * 获取最大编号
     *
     * @param publishLog 发布日志
     * @return 结果
     */
    @Override
    public int findMaxSort(PublishLog publishLog) {
        return NumberUtils.nextOrder(mapper.findMaxSort(publishLog));
    }

    @Transactional(readOnly = false)
    @Override
    public PublishLog findOneInDays() {
        PublishLog publishLog = new PublishLog();
        PublishLog theNewPublishLog = mapper.findOneInDays(publishLog);
        if (null != theNewPublishLog) {
            // 删除之前的发布关联数据
            publishUserService.deletePublishUserHistory(theNewPublishLog.getId());
            PublishUser user = new PublishUser();
            user.setPublishId(theNewPublishLog.getId());
            user.setUserId(SecurityUtils.getLoginUser().getUserId());
            // user.setPublishInDays(3);
            List<PublishUser> existPublishUser = publishUserService.findList(user);
            if (null != existPublishUser && !existPublishUser.isEmpty()) {
                // 如果有数据 但是在三天之前创建的，就不弹框
                Date createDate = existPublishUser.get(0).getCreateTime();
                Calendar calendar = Calendar.getInstance();
                calendar.add(Calendar.DAY_OF_YEAR, -3); // 当前时间减去3天
                Date threeDaysAgo = calendar.getTime();
                if (createDate.before(threeDaysAgo)) {
                    theNewPublishLog = null;
                }
            } else {
                // 如果没有数据 说明 之前没有弹过框， 就弹框
                publishUserService.save(user);
            }
        }
        return theNewPublishLog;
    }
}
